"""
Case Type   : HugePage
Case Name   : enable_huge_pages参数设置为off，系统大页数量、系统大页权限不同场景下，启动数据库验证
Create At   : 2024/11/07
Owner       : liu-tong-8848
Description :
              1、查看系统默认大页
              2、设置系统默认大页数量为0
              3、配置使用大页内存属组
              4、设置enable_huge_pages为on
              5、重启数据库
              6、设置enable_huge_pages为off
              7、重启数据库
              8、设置数据库shared_buffers = 128MB，系统默认大页数量为1000
              9、取消数据库用户使用大页内存属组
              10、设置enable_huge_pages为on
              11、重启数据库
              12、设置enable_huge_pages为off
              13、重启数据库
              14、清理环境
Expect      :
              1、默认为2048kB,成功
              2、成功
              3、成功
              4、成功
              5、重启数据库失败，合理报错
              6、成功
              7、成功
              8、成功
              9、成功
              10、成功
              11、重启数据库失败，合理报错
              12、成功
              13、成功
              14、清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class GucTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.com = Common('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.db_user_node = Node(node='PrimaryDbUser')
        self.db_root_node = Node(node='PrimaryRoot')

    def test_startdb(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')

        step1 = '--step1:查看系统默认大页;expect:默认为2048kB,成功'
        self.log.info(step1)
        cat_hugepagesize_cmd = "grep -i Hugepagesize /proc/meminfo"
        change_hugepages_result = self.db_root_node.sh(cat_hugepagesize_cmd).result()
        self.assertIn('2048 kB', change_hugepages_result, "执行失败" + step1)

        step2 = '--step2:设置系统默认大页数量为0;expect:成功'
        self.log.info(step2)
        change_hugepages_cmd = "echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step2)
        cat_hugepages_cmd = "cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        cat_hugepages_result = self.db_root_node.sh(cat_hugepages_cmd).result()
        self.assertIn('0', cat_hugepages_result, "执行失败" + step2)

        step3 = '--step3:配置使用大页内存属组;expect:成功'
        self.log.info(step3)
        select_groupid_cmd = "id -g"
        groupid = self.db_user_node.sh(select_groupid_cmd).result()
        self.log.info("groupid: " + groupid)
        self.assertNotIn('error', groupid, "执行失败" + step3)
        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=" + groupid
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.assertNotIn('error', change_sysctl_result, "执行失败" + step3)

        step4 = '--step4:打开大页开关，设置enable_huge_pages参数为on;expect:成功'
        self.log.info(step4)
        gs_guc_set_cmd = "enable_huge_pages=on"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step4)

        step5 = '--step5:重启数据库;expect:重启数据库失败，合理报错'
        self.log.info(step5)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertFalse("Normal" in status, "执行失败" + step5)

        step6 = '--step6:设置enable_huge_pages为off;expect:成功'
        self.log.info(step6)
        gs_guc_set_cmd = "enable_huge_pages=off"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step6)

        step7 = '--step7:重启数据库;expect:成功'
        self.log.info(step7)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step7)

        step8 = '--step8:设置数据库shared_buffers = 128MB，系统默认大页数量为1000;expect:成功'
        self.log.info(step8)
        self.log.info("查询shared_buffers默认值")
        self.shared_buffers_original_result = self.com.show_param("shared_buffers")
        gs_guc_set_cmd = "shared_buffers = 128MB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd)
        self.log.info(gs_guc_set_result)
        self.assertTrue(gs_guc_set_result, "执行失败" + step8)
        self.primary_sh.restart_db_cluster()
        result = self.com.show_param("shared_buffers")
        self.assertIn('128MB', result, "执行失败" + step8)
        change_hugepages_cmd = "echo 1000 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.log.info(change_hugepages_result)
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step8)

        step9 = '--step9:数据库用户属组没有使用大页权限配置;expect:成功'
        self.log.info(step9)
        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=1000"
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.assertNotIn('error', change_sysctl_result, "执行失败" + step9)

        step10 = '--step10:打开大页开关，设置enable_huge_pages参数为on;expect:成功'
        self.log.info(step10)
        gs_guc_set_cmd = "enable_huge_pages=on"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                        single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step10)

        step11 = '--step11:重启数据库;expect:重启数据库失败，合理报错'
        self.log.info(step11)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertFalse("Normal" in status, "执行失败" + step11)

        step12 = '--step12:设置enable_huge_pages为off;expect:成功'
        self.log.info(step12)
        gs_guc_set_cmd = "enable_huge_pages=off"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                        single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step12)

        step13 = '--step13:重启数据库;expect:成功'
        self.log.info(step13)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step13)

    def tearDown(self):
        step14 = '--step14:清理环境;expect:成功'
        self.log.info(step14)
        gs_guc_set_cmd = f"shared_buffers=" + self.shared_buffers_original_result
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd)
        self.assertTrue(gs_guc_set_result, "执行失败" + step14)
        gs_guc_set_cmd = f"enable_huge_pages = off"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd)
        self.assertTrue(gs_guc_set_result, "执行失败" + step14)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step14)
        enable_huge_pages = self.com.show_param("enable_huge_pages")
        self.log.info(enable_huge_pages)
        self.assertEqual('off', enable_huge_pages, "执行失败" + step14)
        change_hugepages_cmd = "echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step14)
        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=0"
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.assertNotIn('error', change_sysctl_result, "执行失败" + step14)
        self.log.info(change_sysctl_result)
        self.assertIn('vm.hugetlb_shm_group', change_sysctl_result, "执行失败" + step14)
        use_sysctl_cmd = "sysctl -p"
        self.db_root_node.sh(use_sysctl_cmd)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
